//
//  CLRuntimeViewController.m
//  Charles
//
//  Created by Charles on 2017/2/7.
//  Copyright © 2017年 charles. All rights reserved.
//

#import "CLRuntimeViewController.h"
#import "CLMethod.h"
#import "CLProperty.h"
#import "CLMessage.h"
#import "CLForwardMessage.h"
#import "Runtime.h"
#import "Aspects.h"

#import <objc/runtime.h>
#import "CLOption.h"

@interface CLRuntimeViewController ()

@end


@implementation CLRuntimeViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [CLRuntimeViewController aspect_hookSelector:@selector(testAspects) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> info) {
        NSString *className = NSStringFromClass([[info instance] class]);
        NSLog(@"在调用testAspects之前先调用了block %@",className);
    } error:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //    [CLMethod test];
    
    //    [CLProperty test];
    
    //    [CLMessage test];
    
//    [CLForwardMessage test];
//    UILabel * label = [[UILabel alloc]initWithFrame:self.view.bounds];
//    label.font = [UIFont fontWithName:@"DINNextLTPro-UltraLightIt" size:40];
//    label.textAlignment = NSTextAlignmentCenter;
//    [self.view addSubview:label];
//    label.text = @"运行时测试代码";
//    UIImage * image = [UIImage imageNamed:@"bubble"];
//    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
//    imageView.center = self.view.center;
//    imageView.image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(20, 20, 40, 60) resizingMode:UIImageResizingModeStretch];
//    [self.view addSubview:imageView];
//    
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        CGRect fr = imageView.frame;
//        fr.size.width = 300;
//        fr.size.height = 200;
//        imageView.frame = fr;
//        imageView.center = self.view.center;
//    });
    
//    [Runtime test:@"number"];
//    [Runtime testMethod];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        [self testAspects];
//    });
//
//    Person *person = [[Person alloc]init];
//
//    Method methodNew = class_getInstanceMethod(self.class, @selector(customImp));
//    IMP methodIMPNew = method_getImplementation(methodNew);
//    const char *typeEncoding = method_getTypeEncoding(methodNew);
//
//    IMP replaceImp = class_replaceMethod(person.class, @selector(run), methodIMPNew, typeEncoding);
//
//    NSLog(@"methodIMPNew =%p,replaceImp = %p",methodIMPNew,replaceImp);
//    [person run];
//    [self customImp];
    
//    [CLOption test];
    [CLMethodExchange testMethodExchange];
    [Runtime testClassMethodWithOutImplementation];
}

- (void)customImp {
    NSLog(@"%s",__func__);
}

- (void)testAspects {
    NSLog(@"fuck your uncle");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

@implementation Person

- (void)run {
    NSLog(@"%s",__func__);
}

@end
